package com.soilflow.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.annotation.DataScope;
import com.soilflow.common.domain.LandNotice;
import com.soilflow.common.domain.dto.LandNoticeDto;
import com.soilflow.common.domain.dto.NewsDto;
import com.soilflow.common.domain.vo.LandNoticeVo;
import com.soilflow.common.domain.vo.NewsVO;
import com.soilflow.common.utils.StringUtils;
import com.soilflow.info.mapper.LandNoticeMapper;
import com.soilflow.info.service.LandNoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LandNoticeServiceImpl extends ServiceImpl<LandNoticeMapper, LandNotice> implements LandNoticeService {

    @Resource
    private LandNoticeMapper landNoticeMapper;

    @DataScope(deptAlias = "ln")
    @Override
    public List<LandNoticeVo> landNoticeList(LandNoticeDto dto) {
        List<LandNoticeVo> vos = landNoticeMapper.landNoticeList(dto);
        return vos;
    }

    @Override
    public Boolean addLandNotice(LandNoticeDto dto) {
        //1 查询公告信息
        LambdaQueryWrapper<LandNotice> query = new LambdaQueryWrapper<>();
        query.eq(StringUtils.isNotEmpty(dto.getTitle()),LandNotice::getTitle,dto.getTitle()).last("limit 1");

        if (ObjectUtils.isEmpty(landNoticeMapper.selectOne(query))){
            LandNotice landNotice = new LandNotice();
            BeanUtils.copyProperties(dto,landNotice);
            landNoticeMapper.insert(landNotice);
            return true;
        }

        return false;


    }

    @Override
    public void delLandNotice(List<Long> ids) {
        landNoticeMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateLandNotice(LandNoticeDto dto) {
        LandNotice landNotice = new LandNotice();
        BeanUtils.copyProperties(dto,landNotice);
        landNotice.setId(dto.getId());
        landNoticeMapper.updateById(landNotice);
    }

    @Override
    public LandNotice getLandNoticeById(Long id) {
        LandNotice landNotice = landNoticeMapper.selectById(id);
        return landNotice;
    }

    @Override
    public  List<LandNoticeVo> noticeList(LandNoticeDto dto) {
        List<LandNoticeVo> vos = landNoticeMapper.landNoticeList(dto);
        return vos;
    }

    @Override
    public int number() {
        return this.baseMapper.number();
    }

    @Override
    public int announcements() {
        return this.baseMapper.announcements();
    }

    @Override
    public int notice() {
        return this.baseMapper.notice();
    }

}
